package com.nf.health.app.datamain.activity;


import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.HistoryDataAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.ChartView;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.customview.DragListView.onRefreshAndLoadMoreListener;
import com.nf.health.app.models.HistoryVo;
import com.nf.health.app.models.Sleep;
import com.nf.health.app.models.SleepDelayModel;
import com.nf.health.app.utils.CompareUtil;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/** 
 * @ClassName: 手环历史记录
 * @Description: TODO
 * @author Ian 
 * @date 2016-2-22 上午9:53:01 
 *  
 */
public class HistoryWristActivity extends BaseActivity implements OnMyClickListener{

	static int a = 0;


	int i;

	static List<String> list = new ArrayList<String>();


	static List<Integer> list1 = new ArrayList<Integer>();

	static Map<String, Object> map = new TreeMap<String, Object>();

	LinearLayout layout;

	int position1;

	private int pageNo = 1;
	private String pagesize = "20";

	HistoryDataAdapter historyDataAdapter;
	private DragListView history_listview;

	LinearLayout ll;
	List<HistoryVo> extra;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.layout_common_header);

		setContent(R.layout.activity_health_wrist);

		ViewUtil.setText(this, R.id.titlebar_title_tv, "历史数据");

		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		history_listview = (DragListView) findViewById(R.id.history_listview);
		history_listview.setRefreshableAndLoadMoreable(true, true);

		initData("requestDataDetails");
	}

	@SuppressWarnings("unchecked")
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);


		if(actionName.equals("requestDataDetails")){
			history_listview.completeRefresh();
			extra = (List<HistoryVo>) object;
			if(historyDataAdapter == null){
				historyDataAdapter = new HistoryDataAdapter(this,extra);
				history_listview.setAdapter(historyDataAdapter);
			}else {
				historyDataAdapter.setMyDatanotifyChanged(extra);
			}
			history_listview.setAdapter(historyDataAdapter);
			historyDataAdapter.setOnMyClickListener(this);

		}else if(actionName.equals("Delete_History")){
			showToast("删除成功");
			extra.remove(position1);
			historyDataAdapter.notifyDataSetChanged();//更新数据
		}else if("more".equals(actionName)){
			history_listview.completeLoadMore();
			List<HistoryVo> extra1 = (List<HistoryVo>) object;
			if(extra1 == null || extra1.equals("[]") || extra1.size()<1){
				history_listview.setRefreshableAndLoadMoreable(true, false);
				showToast("没有更多数据");
			}else{
				extra.addAll(extra1);
				if(historyDataAdapter == null){
					historyDataAdapter = new HistoryDataAdapter(this,extra);
					history_listview.setAdapter(historyDataAdapter);
				}else {
					historyDataAdapter.setMyDatanotifyChanged(extra);
				}
				historyDataAdapter.setOnMyClickListener(this);

			}
		}


		history_listview.setOnRefreshAndLoadMoreListener(new onRefreshAndLoadMoreListener() {

			@Override
			public void onRefresh() {
				history_listview.setRefreshableAndLoadMoreable(true, true);
				pageNo = 1;
				initData("requestDataDetails");
			}

			@Override
			public void onLoadMore() {
				pageNo++;
				initData("more");
			}
		});

	}
	// onrestart 进入此页面刷新方法
	@SuppressWarnings("unchecked")
	protected void onResume() {
		super.onResume();
	}


	private void initData(String flag) {
		provider.requestDataHistory("bracelet",pageNo+"",pagesize,flag);
	}
	
	private void showSleepDetail(int position) {
		final AlertDialog dialog;
		AlertDialog.Builder builder = new Builder(HistoryWristActivity.this);
		LayoutInflater factory = LayoutInflater.from(HistoryWristActivity.this);
		final View textEntryView = factory.inflate(R.layout.dialog_layout, null);

		ll = (LinearLayout) textEntryView.findViewById(R.id.lin);


		TextView qxcs = (TextView) textEntryView.findViewById(R.id.qxcs);//清醒次数
		TextView qxsc = (TextView) textEntryView.findViewById(R.id.qxsc);//清醒时长
		TextView sssc = (TextView) textEntryView.findViewById(R.id.sssc);//深睡时长
		TextView qssc = (TextView) textEntryView.findViewById(R.id.qssc);//浅睡时长
		TextView smzl = (TextView) textEntryView.findViewById(R.id.smzl);//睡眠质量
		TextView smsc = (TextView) textEntryView.findViewById(R.id.smsc);//睡眠时长
		TextView zdsj = (TextView) textEntryView.findViewById(R.id.zdsj);//总的时间

		Sleep sleepdetail = extra.get(position).getSleepdetail();
		if(extra.get(position).getSleepdetail() != null){
			try {
				double qxsc_time = Double.parseDouble(sleepdetail.getAwakeDuration());
				qxsc.setText("清醒时长 : "+String.format("%.2f", qxsc_time/60f)+"h");
				double sssc_time = Double.parseDouble(sleepdetail.getDeepDuration());
				sssc.setText("深睡时长 : "+String.format("%.2f", sssc_time/60f)+"h");
				double qssc_time = Double.parseDouble(sleepdetail.getLightDuration());
				qssc.setText("浅睡时长 : "+String.format("%.2f", qssc_time/60f)+"h");
				double zdsj_time = Double.parseDouble(sleepdetail.getTotalDuration());
				zdsj.setText("总的时间 : "+String.format("%.2f", zdsj_time/60f)+"h");
				double sleepDrution = sssc_time + qssc_time;
				double cmsc_time = Double.parseDouble(sleepdetail.getSleepDuration());
				smsc.setText("睡眠时长 : "+String.format("%.2f", sleepDrution/60f)+"h");
				qxcs.setText("清醒次数 : "+sleepdetail.getAwakeCount());
				smzl.setText("睡眠质量 : "+sleepdetail.getQuality());
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(sleepdetail.getDetail()!=null && sleepdetail.getDetail().size()>0){
				List<SleepDelayModel> list = new ArrayList<SleepDelayModel>();
				//Map<String, Object> map= CompareUtil.sortMapByKey(sleepdetail.getDetail());
				ArrayList<String> keyList =  CompareUtil.sortMapByKeyList(sleepdetail.getDetail());

			   for (int i = 0; i <keyList.size() ; i++) {
					long currentLongtime = Long.parseLong(keyList.get(i));
					long nextLongtime = Long.parseLong(keyList.get(i));
					int delay = 0;
					if(i < keyList.size() -1){
						nextLongtime = Long.parseLong(keyList.get(i+1));

					}
				   int  status = (int) Math.round(Double.parseDouble(String.valueOf(sleepdetail.getDetail().get("" + currentLongtime))));
					delay = (int) ((nextLongtime - currentLongtime)) / 10;
					String key = currentLongtime+"";
				   SleepDelayModel model = new SleepDelayModel();
					if(keyList.size() == 1){
						model.setCurrentLongTimre(currentLongtime);
						model.setNextLongTime(nextLongtime);
						model.setDelay(delay);
						model.setStatus(status);
						list.add(model);
					} else {
						model.setCurrentLongTimre(currentLongtime);
						model.setNextLongTime(nextLongtime);

						model.setCurretTime(currentLongtime);
						model.setNextTime(nextLongtime);

						model.setDelay(delay);
						model.setStatus(status);
						list.add(model);
					}
				}
				ll.addView(new ChartView(this, list));
			}
		}


		builder.setView(textEntryView);
		dialog = builder.create();
		dialog.show();				
	}

	@Override
	public void onClick(int position, String flag) {
		if("sm".equals(flag)){
			showSleepDetail(position);
		}else{
			position1 = position;
			provider.requestDeleteHistory("bracelet",extra.get(position).getId(),"Delete_History");
		}
	}
}
